// 3D_Hand.cpp : Defines the entry point for the console application.
//
#include "ParticleSwarm.h"
#include <stdio.h>
#include <conio.h>
#include <vector>
#include "Util.h"
/* CAREFUL WITH OPENGL & OPENCL */


GLFWwindow * mWindow;
OpenCLManager oclManager;

//void main()
//{
//	HandModelRenderer renderer;
//	renderer.window=mWindow;
//
//	SDKTimer sampleTimer;
//	int timex = sampleTimer.createTimer();
//	sampleTimer.resetTimer(timex);
//
//	renderer.initializeGLContext();
//	double tempoArray[23]={0,0,0,	50,60,45,30,	0,10,10,10,
//		0,15,0,0,	0,20,10,0,	0,20,10,0};
//	double tempoArray2[23]={0,0,0,	50,60,45,30,	0,45,20,10,
//		0,15,0,0,	0,20,10,0,	0,20,10,0};
//
//	HypothesisX hypo;
//	for (int i =0;i<23;i++){
//		hypo.dofData[i] = tempoArray[i];
//	}
//
//	HandMeasureFunction function(&renderer,&oclManager);
//	function.generateObserve(hypo);
//	previewTexture2D(256,256*64,renderer.color_tex2d);
//
//	for (int i =0;i<23;i++){
//		hypo.dofData[i] = tempoArray2[i];
//	}
//	function.generateObserve(hypo);
//	previewTexture2D(256,256*64,renderer.color_tex2d);
//
//	for (int i =0;i<23;i++){
//		hypo.dofData[i] = tempoArray[i];
//	}
//
//	function.generateObserve(hypo);
//	previewTexture2D(256,256*64,renderer.color_tex2d);
//}

void main()
{
	HandModelRenderer renderer;
	renderer.window=mWindow;

	SDKTimer sampleTimer;
	int timex = sampleTimer.createTimer();
	sampleTimer.resetTimer(timex);

	renderer.initializeGLContext();
	
	double tempoArray[23]={
		0,0,0,	
		50,60,30,20,	
		0,0,0,0,
		0,55,45,30,	
		0,55,45,30,	
		0,55,45,30};

	HypothesisX obsHypo;
	for (int i =0;i<23;i++)	obsHypo.dofData[i] = tempoArray[i];
	HandMeasureFunction function(&renderer,&oclManager);
	
	function.generateObserve(obsHypo);
	
	int iteration = 0;
	int maxGeneration = 100;
	ParticleSwarm swarm;
	swarm.InitSwarm(&function); // Init the first generation

	sampleTimer.startTimer(timex);
	while (iteration < maxGeneration)
	{
		swarm.Iteration();
		iteration++;
		
		
		if (iteration%3 == 0)
		{
			swarm.Randomization(0.3,vPOS_THUMB,vPOS_DOF);
			//printf("Randomize %d \n", iteration/3);
		}
		printf("Best cost of Generation %d: \t %.2f \n",iteration, swarm.CurrentgBestCost());
		HypothesisX tmp = PositionXToHypo(swarm.gBestPosition);
		renderer.renderHandModelGL_Comparing(obsHypo,tmp);
	}
	sampleTimer.stopTimer(timex);
	double timeCL = sampleTimer.readTimer(timex);
	printf("time = %f\n",timeCL);
}